<template>
  <div class="mod-role">
    <el-form
      :inline="true"
      :model="queryInfo"
      @keyup.enter.native="getDataList()"
    >
      <el-form-item>
        <el-input
          v-model="queryInfo.example.roleName"
          :placeholder="i18n['sys.role.field.roleName.title']"
          clearable
        ></el-input>
      </el-form-item>
      <el-form-item>
        <el-button @click="getDataList()">{{
          i18n["gobal.button.search"]
        }}</el-button>
        <el-button
          v-if="isAuth('sys:role:insert')"
          type="primary"
          @click="addOrUpdateHandle()"
          >{{ i18n["gobal.button.insert"] }}</el-button
        >
        <el-button
          v-if="isAuth('sys:role:delete')"
          type="danger"
          @click="deleteHandle()"
          :disabled="dataListSelections.length <= 0"
          >{{ i18n["gobal.button.delete-multi"] }}</el-button
        >
      </el-form-item>
    </el-form>
    <el-table
      :data="dataList"
      stripe
      border
      @selection-change="selectionChangeHandle"
      style="width: 100%"
    >
      <el-table-column
        type="selection"
        header-align="center"
        align="center"
        width="50"
      >
      </el-table-column>
      <el-table-column
        prop="roleId"
        header-align="center"
        align="center"
        width="80"
        :label="i18n['sys.role.field.roleId.title']"
        v-if="false"
      >
      </el-table-column>
      <el-table-column
        prop="roleName"
        header-align="center"
        align="center"
        :label="i18n['sys.role.field.roleName.title']"
      >
      </el-table-column>
      <el-table-column
        prop="disableFlag"
        header-align="center"
        align="center"
        :label="i18n['gobal.field.disableFlag']"
      >
        <template slot-scope="scope">
          <el-tag
            v-if="scope.row.disableFlag === 1"
            size="small"
            type="danger"
            >{{ i18n["gobal.field.disableFlag.YES"] }}</el-tag
          >
          <el-tag v-else size="small">{{
            i18n["gobal.field.disableFlag.NO"]
          }}</el-tag>
        </template>
      </el-table-column>
      <el-table-column
        prop="defaultFlag"
        header-align="center"
        align="center"
        :label="i18n['gobal.field.defaultFlag']"
      >
        <template slot-scope="scope">
          <el-tag
            v-if="scope.row.defaultFlag === 1"
            size="small"
            type="danger"
            >{{ i18n["gobal.field.defaultFlag.YES"] }}</el-tag
          >
          <el-tag v-else size="small">{{
            i18n["gobal.field.defaultFlag.NO"]
          }}</el-tag>
        </template>
      </el-table-column>
      <el-table-column
        prop="description"
        header-align="center"
        align="center"
        :label="i18n['gobal.field.description']"
      >
      </el-table-column>
      <el-table-column
        header-align="center"
        align="center"
        :label="i18n['gobal.tips.operation']"
      >
        <template slot-scope="scope">
          <el-button
            v-if="isAuth('sys:role:update')"
            type="primary"
            size="small"
            @click="addOrUpdateHandle(scope.row.roleId)"
            >{{ i18n["gobal.button.update"] }}</el-button
          >
          <el-button
            v-if="isAuth('sys:role:delete')"
            type="danger"
            size="small"
            @click="deleteHandle(scope.row.roleId)"
            :disabled="scope.row.isDefault === 1"
            >{{ i18n["gobal.button.delete"] }}</el-button
          >
        </template>
      </el-table-column>
    </el-table>

    <el-pagination
      @size-change="sizeChangeHandle"
      @current-change="currentChangeHandle"
      :current-page="queryInfo.page.pageNo"
      :page-size="queryInfo.page.pageSize"
      :total="queryInfo.page.dataCount"
      layout="total,  prev, pager, next, jumper"
    >
    </el-pagination>
    <!-- 弹窗, 新增 / 修改 -->
    <add-or-update
      v-if="addOrUpdateVisible"
      ref="addOrUpdate"
      @refreshDataList="getDataList"
    ></add-or-update>
  </div>
</template>

<script>
import AddOrUpdate from "./role-add-or-update";
export default {
  inject: ["i18n"],
  data() {
    return {
      queryInfo: {
        example: {
          roleName: "",
        },
        page: {
          pageNo: 1,
          pageSize: 20,
          dataCount: null,
        },
      },
      dataList: [],
      dataListSelections: [],
      addOrUpdateVisible: false,
    };
  },
  components: {
    AddOrUpdate,
  },
  activated() {
    this.getDataList();
  },
  methods: {
    // 获取数据列表
    getDataList() {
      this.$http({
        url: this.$http.adornUrl("/sys/role/list"),
        method: "post",
        data: this.$http.adornData({
          page: this.queryInfo.page,
          example: this.queryInfo.example,
        }),
      }).then(({ data }) => {
        if (data && data.code === 200 && data.data && data.data.list) {
          this.dataList = data.data.list;
          this.queryInfo.page.dataCount = Number(data.data.page.dataCount);
        } else {
          this.dataList = [];
          this.queryInfo.page.dataCount = 0;
        }
      });
    },
    // 每页数
    sizeChangeHandle(val) {
      this.queryInfo.page.pageSize = val;
      this.queryInfo.page.pageNo = 1;
      this.getDataList();
    },
    // 当前页
    currentChangeHandle(val) {
      this.queryInfo.page.pageNo = val;
      this.getDataList();
    },
    // 多选
    selectionChangeHandle(val) {
      this.dataListSelections = val;
    },
    // 新增 / 修改
    addOrUpdateHandle(id) {
      this.addOrUpdateVisible = true;
      this.$nextTick(() => {
        this.$refs.addOrUpdate.init(id);
      });
    },
    // 删除
    deleteHandle(id) {
      var ids = id
        ? [id]
        : this.dataListSelections.map((item) => {
            return item.roleId;
          });
      this.$confirm(
        i18n["sys.role.delete.ask-hint"],
        i18n["gobal.dialog.hint"],
        {
          confirmButtonText: i18n["gobal.button.ok"],
          cancelButtonText: i18n["gobal.button.cancel"],
          type: "warning",
        }
      )
        .then(() => {
          this.$http({
            url: this.$http.adornUrl("/sys/role/delete"),
            method: "post",
            data: this.$http.adornData(ids, false),
          }).then(({ data }) => {
            if (data && data.code === 200) {
              this.$message({
                message: i18n["gobal.hint.succeeded"],
                type: "success",
                duration: 1500,
                onClose: () => {
                  this.getDataList();
                },
              });
            } else {
              this.$message.error(data.msg);
            }
          });
        })
        .catch(() => {});
    },
  },
};
</script>
